package com.example.administrator.music;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.R.id.list;

/**
 * Created by Administrator on 2018/3/30.
 */

public class SelectActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView listView;

    private AutoCompleteTextView editText;
    private ImageView delete;
    private LinearLayout select_theme;
    private int number = 0;


    private ArrayList<Music> musiclist;
    List<Map<String, String>> listmap = new ArrayList<Map<String, String>>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);
        initview();
        initlistdemoView();
        settheme();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    private void initview() {
        select_theme = (LinearLayout) findViewById(R.id.select_theme);
        editText = (AutoCompleteTextView) findViewById(R.id.select_edittext);
        delete = (ImageView) findViewById(R.id.select_delet);
        delete.setOnClickListener(this);

        listView = (ListView) findViewById(R.id.selectlist);


        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.VISIBLE);
                    listmap.clear();
                    String date = String.valueOf(s);
                    getmusiclist(date);
                } else {
                    delete.setVisibility(View.INVISIBLE);
                    listView.setVisibility(View.INVISIBLE);
                }

            }
        });
    }


    private void getmusiclist(String date) {

        HashMap<String, String> map;
        SimpleAdapter simpleAdapter;
        int length = musiclist.size();
        for (int i = 0; i < length; i++) {
            if (musiclist.get(i).getMusicName().contains(date) || musiclist.get(i).getMusicArtist().contains(date)) {
                map = new HashMap<String, String>();
                map.put("musicName", musiclist.get(i).getMusicName());
                map.put("musicArtist", musiclist.get(i).getMusicArtist());
                listmap.add(map);
            }
        }
        String[] from = new String[]{"musicName", "musicArtist"};
        int[] to = {R.id.listview_songname, R.id.listview_artname};
        simpleAdapter = new SimpleAdapter(this, listmap, R.layout.listviewdemo, from, to);
        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = listmap.get(position).get("musicName");
                String art = listmap.get(position).get("musicArtist");
                int length = musiclist.size();
                for (int i = 0; i < length; i++) {
                    if (musiclist.get(i).getMusicName().equals(name) && musiclist.get(i).getMusicArtist().equals(art)) {
                        number = i;
                    }
                }
                sendBroadcastOnCommand(MusicService.COMMAND_PLAY);
            }
        });
    }

    //可被调用的发送广播的函数
    private void sendBroadcastOnCommand(int command) {
        Intent intent = new Intent(MusicService.BROADCAST_MUSICSERVICE_CONTROL);
        intent.putExtra("command", command);
        intent.putExtra("number", number);
        sendBroadcast(intent);
    }

    //将音乐添加到数组中的函数
    private void initlistdemoView() {
        musiclist = Musiclist.getMusiclist();
        //避免重复添加音乐
        if (musiclist.isEmpty()) {
            Cursor mMusicCursor = this.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                    MediaStore.Audio.AudioColumns.TITLE);
            //标题
            int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
            //艺术家
            int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
            //总时长
            int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);
            //路径
            int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);

            /**通过mMusicCursor游标遍历数据库，并将Music类对象加载到ArrayList中*/
            for (mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor.moveToNext()) {
                String strTitle = mMusicCursor.getString(indexTitle);
                String strArtist = mMusicCursor.getString(indexArtist);
                String strTotoalTime = mMusicCursor.getString(indexTotalTime);
                String strPath = mMusicCursor.getString(indexPath);

                if (strArtist.equals("<unknown>"))
                    strArtist = "无艺术家";
                Music music = new Music(strTitle, strArtist, strPath, strTotoalTime);
                musiclist.add(music);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.select_delet:
                editText.setText("");
                break;
        }

    }

    private void settheme() {
        Intent getint = getIntent();
        int tmbg = getint.getIntExtra("theme", 0);
        switch (tmbg) {
            case MenuActivity.THEME_MENGLONG:
                select_theme.setBackgroundResource(R.drawable.bg_blur);
                break;
            case MenuActivity.THEME_BINGXUE:
                select_theme.setBackgroundResource(R.drawable.bg_snow);
                break;
            case MenuActivity.THEME_CAISE:
                select_theme.setBackgroundResource(R.drawable.bg_color);
                break;
            case MenuActivity.THEME_HUADUO:
                select_theme.setBackgroundResource(R.drawable.bg_digit_flower);
                break;
            case MenuActivity.THEME_QUNSHANG:
                select_theme.setBackgroundResource(R.drawable.bg_mountain);
                break;
        }
    }

}
